package com.lemon.smartsafety.ui.adapter.profile;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.base.AppEvent;
import com.lemon.smartsafety.entity.user.ProfileBaseModel;
import com.lemon.smartsafety.entity.user.ProfileCommonModel;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/1.
 */
public class ProfileCommon implements AdapterItem<ProfileBaseModel> {
    private LoggerHelper loggerHelper = new LoggerHelper(ProfileCommon.class);

    private boolean isFirst = false, showArrow = true;

    private RelativeLayout mItemMainLayout;
    private View mTopDivider, mBottomDivider;
    private ImageView mImageIcon, mImageArrow;
    private TextView mItemText, mItemValue;
    private LinearLayout ripple;


    @Override
    public int getLayoutResId() {
        return R.layout.item_profile_common;
    }

    @Override
    public void bindViews(View root) {
        ripple = (LinearLayout) root.findViewById(R.id.ripple);
        mItemMainLayout = (RelativeLayout) root.findViewById(R.id.itemMainLayout);
        mTopDivider = root.findViewById(R.id.top_divider);
        mBottomDivider = root.findViewById(R.id.bottom_divider);
        mImageIcon = (ImageView) root.findViewById(R.id.imageIcon);
        mImageArrow = (ImageView) root.findViewById(R.id.imageArrow);
        mItemText = (TextView) root.findViewById(R.id.itemText);
        mItemValue = (TextView) root.findViewById(R.id.itemSubText);
    }

    @Override
    public void setViews() {
        setDivider();
    }

    private void setDivider() {
        if (isFirst) {
            mTopDivider.setVisibility(View.VISIBLE);
            mBottomDivider.setVisibility(View.VISIBLE);
        } else {
            mTopDivider.setVisibility(View.INVISIBLE);
            mBottomDivider.setVisibility(View.VISIBLE);
        }
    }

    private void setArrow() {
        if (showArrow) {
            mImageArrow.setVisibility(View.VISIBLE);
        } else {
            mImageArrow.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void handleData(ProfileBaseModel profileBaseModel, int position) {
        if (profileBaseModel.getViewType() == ProfileViewType.TYPE_COMMON_ITEM) {
            final ProfileCommonModel common = (ProfileCommonModel) profileBaseModel;

            isFirst = common.isFirst();
            setDivider();

            showArrow = common.isShowArrow();
            setArrow();


            mItemText.setText(mItemText.getContext().getString(common.getTitle()));
            if (common.getIcon() != 0) {
                mImageIcon.setImageResource(common.getIcon());
                mImageIcon.setVisibility(View.VISIBLE);
            } else {
                mImageIcon.setVisibility(View.GONE);
            }
            //设置默认值
            if (StringUtils.isEmpty(common.getValue())) {
                mItemValue.setVisibility(View.GONE);
                mItemValue.setText("");
            } else {
                mItemValue.setVisibility(View.VISIBLE);
                mItemValue.setText(StringUtils.nullValue(common.getValue()));
            }
//            mItemMainLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    AppEvent event = new AppEvent();
//                    event.setTag(common.getTag().toString());
//                    event.setEventResult(Constants.Result.SUCCESS);
//                    AppService.getBus().post(event);
//                }
//            });
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AppEvent event = new AppEvent();
                    event.setTag(common.getTag().toString());
                    event.setEventResult(Constants.Result.SUCCESS);
                    AppService.getBus().post(event);
                }
            });
        /*    ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    AppEvent event = new AppEvent();
                    event.setTag(common.getTag().toString());
                    event.setEventResult(Constants.Result.SUCCESS);
                    AppService.getBus().post(event);
                }
            });*/
        }
    }
}
